#include <iostream>
#include <string>
#include "Dekart.h"

void Process( Dekart& dekartTree ) {
	char c;
	try {
 		while( true ) {
			std::string str;
			while( ( c = getchar() ) != ' ' ) {
				if( c == '\n' )
					break;
				str += c;
			}
			if( str == "add" ) {
				int data;
				std::cin >> data;
				dekartTree.Add( data );
			} 
			else if( str == "delete" ) {
				int index;
				std::cin >> index;
				dekartTree.Delete( index );
			} 
			else if( str == "insert" ) {
				int data, index;
				std::cin >> index >> data;
				dekartTree.Insert( index, data );
			} 
			else if( str == "sum" ) {
				int a, b;
				std::cin >> a >> b;
				std::cout << dekartTree.Sum( a, b + 1 ) << std::endl;
			}
			else if( str == "exit" ) {
				return;
			}
			else {
				std::cout << "Unknown command" << std::endl;
				continue;
			}
			getchar();
		}
	} catch( Exception& exc ) {
		std::cout << std::string( exc.GetMessage() ) << std::endl;
	}
}

int main() {
	Dekart dekartTree;
	Process( dekartTree );
	return 0;
}